<template>
  <div>
    <el-card v-loading="loading">

      <avue-crud :data="data" :option="option" @refresh-change="refreshChange" @row-save="rowSave" @row-update="rowUpdate"
        @row-del="rowDel" v-model:page="page" @size-change="sizeChange" @current-change="currentChange"
        @search-change="searchChange">



      </avue-crud>
    </el-card>

  </div>
</template>

<script setup>
import { ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { addclient, deleteclient, updateclient, manageclient, xuserControllerxuserController } from '@/api/user.ts'
import { getXfactoryController } from '@/api/firm.ts'
import { useUserStore } from '@/store/modules/user'
import { onMounted } from 'vue'

const { userMessage } = useUserStore()

const loading = ref(true)
const data = ref([]);


const page = ref({
  total: 0,
  currentPage: 1,
  pageSize: 10
})


const getData = () => {
  loading.value = true

  let Q = {
    index: page.value.currentPage,
    size: page.value.pageSize,
  }


  manageclient(Q).then(res => {
    data.value = res.result
    page.value.total = res.page.total
    loading.value = false
  })
}


getData()


const factoryidArr = ref([])

getXfactoryController({
  index: 1,
  size: 100,
}).then(res => {
  if (res.result && res.result.length > 0) {
    factoryidArr.value = res.result.map(v => {
      return {
        label: v.name,
        value: v.id
      }
    })

    option.value.column[4].dicData = factoryidArr.value

  }
})


const option = ref({
  searchMenuSpan: 18,
  viewBtn: true,
  column: [
    { label: '用户名', prop: 'username', rules: [{ required: true, message: '请输入用户名', trigger: 'blur' }] },
    { label: '密码', prop: 'password', rules: [{ required: true, message: '请输入密码', trigger: 'blur' }] },
    { label: '姓名', prop: 'name', rules: [{ required: true, message: '请输入姓名', trigger: 'blur' }] },
    { label: '邮箱', prop: 'email' },
    {
      label: '厂商', prop: 'factoryid', type: 'select',
      search: true,
      dicData: [],
      formatter: (row, value) => {
        let item = factoryidArr.value.find(v => v.value === value)
        return item?.label || '暂无绑定'
      }
    },
    { label: 'openid', prop: 'openid' },
    { label: '人员电话', prop: 'phone', rules: [{ required: true, message: '请输入人员电话', trigger: 'blur' }] },
    { label: 'sessionkey', prop: 'sessionkey' },
    { label: 'unionid', prop: 'unionid' },
    { label: '创建时间', prop: 'createTime', display: false },
    {
      label: '权限',
      prop: 'qx',
      type: 'switch',
      dicData: [
        { label: '无权限', value: 0 },
        { label: '有权限', value: 1 },
      ],
      value: 0
    }
  ]
});



const refreshChange = () => {
  getData()
};

const rowSave = (form, done, loading) => {
  // loading();
  addclient(form).then(res => {
    if (res !== 'error') {
      done(form);
      getData();
    } else {
      done();
    }
  }).catch(() => {
    loading()
  })

};

const rowDel = (form, index, done) => {
  ElMessageBox.confirm('是否删除?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    deleteclient(form.id).then(res => {
      ElMessage.success('删除成功!');
      done(form);

    })

  }).catch(() => { });
};


const rowUpdate = (form, index, done, loading) => {

  let Q = Object.assign(form, {})
  Q.userid = userMessage.id
  updateclient(Q).then(res => {
    ElMessage.success('修改成功');
    done(form);
  })
};



function sizeChange(val) {
  page.value.currentPage = 1;
  page.value.pageSize = val;
  getData();
}

function currentChange(val) {
  page.value.currentPage = val;
  getData();
}


function searchChange(params, done) {
  console.log(params, 'params')

  if (!params.factoryid) {
    done();
    return getData()
  }
  loading.value = true
  xuserControllerxuserController(params.factoryid).then(res => {
    console.log(res, 'res')
    data.value = res.result
    page.value.total = 1
    loading.value = false
    done();
  })


}
</script>